home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / interp / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  2.6 KB  |  104 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: input.c,v 1.15 94/10/05 21:02:19 nkramer Exp $
  27. *
  28. * This file implements getc.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "../compat/std-c.h"
  33. #include "../compat/std-os.h"
  34.  
  35. #include "mindy.h"
  36. #include "char.h"
  37. #include "list.h"
  38. #include "bool.h"
  39. #include "thread.h"
  40. #include "func.h"
  41. #include "driver.h"
  42. #include "error.h"
  43. #include "def.h"
  44.  
  45. static void getc_or_wait(struct thread *thread)
  46. {
  47.     if (FBUFEMPTYP(stdin)
  48.         && !feof(stdin)) {
  49.     int fd = fileno(stdin);
  50.     fd_set fds;
  51.     struct timeval tv;
  52.     int nfound;
  53.  
  54.     FD_ZERO(&fds);
  55.     FD_SET(fd, &fds);
  56.     tv.tv_sec = 0;
  57.     tv.tv_usec = 0;
  58.  
  59.     nfound = select(fd+1, &fds, NULL, NULL, &tv);
  60.  
  61.     if (nfound < 0) {
  62.         switch (errno) {
  63.           case EBADF:
  64.         error("Tried to getc with stdin broken.");
  65.           case EINTR:
  66.         wait_for_input(thread, fd, getc_or_wait);
  67.           case EINVAL:
  68.         lose("select failed with EINVAL?");
  69.         }
  70.     }
  71.     else if (nfound == 0)
  72.         wait_for_input(thread, fd, getc_or_wait);
  73.     }
  74.  
  75.     {
  76.     int c = getchar();
  77.     obj_t *old_sp = pop_linkage(thread);
  78.  
  79.     if (c != EOF)
  80.         *old_sp = int_char(c);
  81.     else
  82.         *old_sp = obj_False;
  83.  
  84.     thread->sp = old_sp + 1;
  85.  
  86.     do_return(thread, old_sp, old_sp);
  87.     }
  88. }
  89.  
  90. static obj_t dylan_getc(void)
  91. {
  92.     getc_or_wait(thread_current());
  93.     go_on();
  94.     /* go_on never returns. */
  95.     lose("go_on actually returned?");
  96.     return NULL;
  97. }
  98.  
  99. void init_input_functions(void)
  100. {
  101.     define_function("getc", obj_Nil, FALSE, obj_False, FALSE,
  102.             obj_CharacterClass, dylan_getc);
  103. }
  104.